<script setup>
import {getCurrentInstance, ref} from 'vue';
import{useRouter}from 'vue-router'

// 搜索输入框的值
const searchInput = ref('');
// 演出列表数据
const performanceList = ref([]);
// 是否已经执行过搜索（用于控制无结果提示的显示）
const hasSearched = ref(false);
// 加载状态
const loading = ref(false);

const global = getCurrentInstance().appContext.config.globalProperties;

const router=useRouter();
//编程式路由跳转
const myRouter = () => {
  router.go(-1) // history.back(), history.forward(), history.go(-1|1)
  // router.back()  //后退历史
  // router.forward(); //前进历史
}


// 处理搜索逻辑
const handleSearch = async () => {
  // 清空之前的结果
  performanceList.value = [];
  // 标记为已搜索
  hasSearched.value = true;
  // 显示加载状态
  loading.value = true;

  // 输入为空时不发送请求
  if (!searchInput.value.trim()) {
    loading.value = false;
    return;
  }

  try {
    // 调用后端模糊查询接口
    const response = await global.$http.get('/public/maoyanmovieproject/fuzzyQueryPerformance', {
      params: {
        input: searchInput.value.trim()  // 传递搜索关键词
      }
    });

    // 成功获取数据后更新列表
    performanceList.value = response.data;
  } catch (error) {
    console.error('搜索失败:', error);
    // 可以添加错误提示
    alert('搜索失败，请稍后重试');
  } finally {
    // 无论成功失败都关闭加载状态
    loading.value = false;
  }
};
</script>


<template>
  <div class="fuzzy-search-container">
    <br>
    <!-- 返回图标-->
    <el-row class="header" align="middle">
      <el-col :span="6" align="center">
        <el-icon class="header-back" @click="myRouter">
          <ArrowLeft/>
        </el-icon>
      </el-col>
      <el-col :span="18">
        <div class="mySearch">
          <h3>搜索</h3>
        </div>
      </el-col>
    </el-row>
    <br>
    <!-- 搜索输入框 -->
    <div class="input">
    <el-input
        v-model="searchInput"
        placeholder="请输入演出名、地点或时间进行搜索"
        clearable
        @keyup.enter="handleSearch"
        style="width: 300px; margin-bottom: 20px;"
    >
      <template #append>
        <el-button
            @click="handleSearch"
            :loading="loading"
            icon="Search"
        />
      </template>
    </el-input>
    </div>
    <!-- 搜索结果展示 -->
    <div v-if="performanceList.length > 0 && !loading" class="result-list">
      <!-- 遍历演出列表，每个项作为一行 -->
      <el-card
          v-for="(item, index) in performanceList"
          :key="index"
          class="performance-item"
      >
        <div class="item-content">
          <!-- 左侧：演出图片 -->
          <div class="item-image">
            <el-image
                :src="item.purl"
                alt="演出图片"
                style="width: 120px; height: 160px; object-fit: cover;"
            />
          </div>

          <!-- 右侧：演出信息 -->
          <div class="item-info">
            <h3 class="info-name">{{ item.pname }}</h3>
            <p class="info-location">
              <span class="info-label">演出地点：</span>{{ item.plocation }}
            </p>
            <p class="info-time">
              <span class="info-label">演出时间：</span>{{ item.ptime }}
            </p>
            <p class="info-price">
              <span class="info-label">演出价格：</span>{{ item.pprice }} 元
            </p>
          </div>
        </div>
      </el-card>
    </div>


    <!-- 无结果提示 -->
    <div v-else-if="hasSearched && !loading" class="no-result">
      没有找到匹配的演出信息
    </div>
  </div>
</template>


<style scoped>
.fuzzy-search-container {
  padding: 20px;

}

.no-result {
  text-align: center;
  padding: 50px;
  color: #666;
}

.result-list {
  display: flex;
  flex-direction: column;
  gap: 16px; /* 行与行之间的间距 */
  padding: 10px 0;
}

.performance-item {
  width: 100%;
  border: 1px solid #eee;
  border-radius: 4px;
  overflow: hidden;
  transition: all 0.3s;
}

.performance-item:hover {
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.1); /* 悬停阴影效果 */
}

.item-content {
  display: flex; /* 图片和文字横向排列 */
  padding: 16px;
  align-items: center;
}

.item-image {
  flex-shrink: 0; /* 图片不压缩 */
  margin-right: 20px;
}

.item-info {
  flex-grow: 1; /* 文字区域占满剩余空间 */
  line-height: 1.8;
}

.info-name {
  margin: 0 0 10px 0;
  font-size: 18px;
  color: #333;
}

.info-label {
  color: #666;
  font-weight: 500;
}

/* 响应式调整：小屏幕下图片在上，文字在下 */
@media (max-width: 768px) {
  .item-content {
    flex-direction: column;
    text-align: center;
  }

  .item-image {
    margin-right: 0;
    margin-bottom: 16px;
  }
}
.mySearch{
  padding-left: 60px;
}

.header{
  background-color: white;
}
.input{
  padding-left: 20px;
}
</style>
